package lolic;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * The Items class is a collection of the Item class.  It represents all the items in the game.
 * @author Dave DePiano
 *
 */

public class ItemCollector extends ArrayList<Item>
{
	
	
	
	public ItemCollector(JProgressBar progressBar, Document docItems)
	{
		getItems(progressBar, docItems);
		alphabetize();
	}
	
	
	/**
	 * puts all the Item elements in alphabetical order
	 */
	private void alphabetize() {
		Collections.sort(this, new Comparator<Object>(){
			 
            public int compare(final Object o1, final Object o2) {
                final Item i1 = (Item) o1;
                final Item i2 = (Item) o2;
               return i1.toString().compareToIgnoreCase(i2.toString());
            } 
        });
	}
	
	public ImageIcon getImage(String strName, int intID)
	{
		Image img;
		//first, search for image locally
		File f = new File("items/" + intID + ".gif");
		if(f.exists())
		{
			ImageIcon imgIcon = new ImageIcon("items/" + intID + ".gif");
			return imgIcon;
		}
		else //if not found local, search leagueoflegends.com site
		{
			try {
			    URL url = new URL(Statics.strLolItemPortraitsURL + intID + ".gif");
			    img = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);
			    ImageIcon imgIcon = new ImageIcon(img, intID+"");
			    
			    
			    if(imgIcon.getIconWidth() < 1) //image is not on the site either, use a blank image 
			    {
			    	imgIcon = new ImageIcon("blank.gif", intID+"");
			    	System.err.println("--"+Statics.strLolItemPortraitsURL + intID + ".gif not found");
			    }
			    else
			    {
					// save image from site to file
					BufferedImage bi = new BufferedImage(
							imgIcon.getIconWidth(), imgIcon.getIconHeight(),
							BufferedImage.TYPE_INT_ARGB);
					Graphics2D g2 = bi.createGraphics();
					// Draw img into bi so we can write it to file.
					g2.drawImage(img, 0, 0, null);
					g2.dispose();
					ImageIO.write(bi, "jpg",
							new File("items/" + intID + ".gif"));
				}
			    //System.out.println(intID);
			    return imgIcon;
			} catch (MalformedURLException e) 
			{
				e.printStackTrace();
			} catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
		
		return null;

	}

	/**
	 * This functions reads the XML at Statics.strItemsURL and adds each item name to the Items class
	 * @param progressBar 
	 */
	
	public void getItems(JProgressBar progressBar, Document docItems)
	{			
		docItems.getDocumentElement ().normalize ();        
		NodeList listOfItems =  docItems.getElementsByTagName("Item");
		
		int i = 0;
		
		while(listOfItems.item(i) != null)
		{
			Element eleItem = (Element)listOfItems.item(i);
			int intID = new Integer(eleItem.getAttribute("id"));
			String strName = eleItem.getAttribute("name");
			Item item = new Item(strName, intID);	
			ImageIcon imgIcon = getImage(strName, intID);
			item.setImgPortrait(imgIcon);
			add(item);
			i++;
			progressBar.setValue(i);
		}       
		
		return;

	}
}
